package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.viewmodel.MyInvitationCodeViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.example.common.zxing.encoding.EncodingHandler;

import butterknife.Bind;

/**
 * 我的邀请码
 */
public class MyInvitationCodeActivity extends AbsNetworkStateActivity {

    @Bind(R.id.qr_code_image) ImageView mRqCodeImage;
    @Bind(R.id.copy_download_url) View mCopyDownloaddUrl;
    @Bind(R.id.invitation_code_text) TextView mInvitationCodeText;
    @Bind(R.id.total_invitation_text) TextView mTotalInvitationText;
    @Bind(R.id.copy_invitation_code_text) TextView mCopyInvitationCodeText;

    private MyInvitationCodeViewModel mViewModel;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, MyInvitationCodeActivity.class));
    }

    @Override
    public int getTitleLayoutRes() {
        return R.layout.layout_default_title_bar;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_my_invitation_code;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_my_invitation_code);
        mViewModel = ViewModelProviders.of(this).get(MyInvitationCodeViewModel.class);

        // 用户邀请码及邀请人数
        initInvitationCode();

        // 复制邀请码
        mCopyInvitationCodeText.setOnClickListener(v ->
                Androids.copyToChipBoard(this, mInvitationCodeText.getText().toString())
        );

        // 复制下载连接
        mCopyDownloaddUrl.setOnClickListener(v ->
                Androids.copyToChipBoard(this,
                        "http://www.gbcoin.top/mobile?inviteCode=" + mInvitationCodeText.getText().toString())
        );
    }

    private void generateRqCode(String code){
        try{
            String address = "http://www.gbcoin.top/mobile?inviteCode=" + code;
            Bitmap addressBitmap = EncodingHandler.createQRCode(address,
                    getResources().getDimensionPixelOffset(R.dimen.dp_125));
            mRqCodeImage.setImageBitmap(addressBitmap);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initInvitationCode() {
        mViewModel.getMyInvitationData().observe(this, resource -> {
            Resource.load(this, resource, this, result -> {
                mInvitationCodeText.setText(result.getInvite_code());
                mTotalInvitationText.setText(String.valueOf(result.getInvites()));
                generateRqCode(result.getInvite_code());
            });
        });
    }

    @Override
    public void onAnewRequest() {
        initInvitationCode();
    }

//    private void shareToWeChatMoments() {
//        share(SendMessageToWX.Req.WXSceneTimeline);
//    }
//
//    private void shareToWeChat() {
//        share(SendMessageToWX.Req.WXSceneSession);
//    }
//
//    private void share(int scene) {
//        WXWebpageObject wxWebpageObject = new WXWebpageObject();
//        wxWebpageObject.webpageUrl = "http://www.cipher-ex.com/mobile?inviteCode=" + mInvitationCodeText.getText().toString();
//        WXMediaMessage wxMediaMessage = new WXMediaMessage(wxWebpageObject);
//        wxMediaMessage.title = "CE—全球最快的去中心化交易所";
//        wxMediaMessage.description = "最大的机遇，在您打开时开启，财富翻倍，一触即发。";
//        wxMediaMessage.thumbData = thumb();
//
//        SendMessageToWX.Req req = new SendMessageToWX.Req();
//        req.scene = scene;
//        req.message = wxMediaMessage;
//        req.transaction = String.valueOf(System.currentTimeMillis());
//        App.getInstance().mWxApi.sendReq(req);
//    }
//
//    private byte[] thumb() {
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_share_logo);
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
//        return byteArrayOutputStream.toByteArray();
//    }
}
